import * as fs from 'node:fs/promises';
import * as vscode from 'vscode';
import { ensureTemplateFile } from "./selectTemplateFile";
import { Helper } from '../helper';

const placeholderFileStr = `
// custom value your name or type name, add those comment api in your template file.
/*tav* howToImport : import * as $$_FILE_NAME from "$$_RELATIVE_PATH" *tav*/
/*tav* headerTypeName : HeaderParams *tav*/
/*tav* queryTypeName : QueryParams *tav*/
/*tav* bodyTypeName : BodyParams *tav*/
/*tav* respTypeName : Response *tav*/
/*tav* headerValName : header *tav*/
/*tav* queryValName : params *tav*/
/*tav* bodyValName : body *tav*/

// code block placeholder, add those comment api in your template file.
/*tav* $$_FILE_HEADER_ZONE *tav*/
/*tav* $$_TYPE_DECLARATIONS_ZONE *tav*/

// header value type placeholder
declare type $$_I_HEADER_TYPE = any;

// request url qurey type name placeholder
declare type $$_I_PARAMS_TYPE = any;

// post body type name placeholder
declare type $$_I_BODY_TYPE = any;

declare type $$_RESPONSE_TYPE = any;

declare type $$_FILE_NAME_WITH_SUFFIX = string;
declare type $$_FILE_NAME = string;


// header value name placeholder
declare const $$_I_HEADER_OR_UNDEFINED: $$_I_HEADER_TYPE | undefined
declare const $$_I_HEADER_OR_NULL: $$_I_HEADER_TYPE | null
declare const $$_I_HEADER_OR_EOBJ: $$_I_HEADER_TYPE | {}
declare const $$_I_HEADER_OR_EARR: $$_I_HEADER_TYPE | []
declare const $$_I_HEADER: $$_I_HEADER_TYPE;

// request url qurey value name placeholder
declare const $$_I_PARAMS_OR_UNDEFINED: $$_I_PARAMS_TYPE | undefined
declare const $$_I_PARAMS_OR_NULL: $$_I_PARAMS_TYPE | null
declare const $$_I_PARAMS_OR_EOBJ: $$_I_PARAMS_TYPE | {}
declare const $$_I_PARAMS_OR_EARR: $$_I_PARAMS_TYPE | []
declare const $$_I_PARAMS: $$_I_PARAMS_TYPE;

// post body value name placeholder
declare const $$_I_BODY_OR_UNDEFINED: $$_I_BODY_TYPE | undefined
declare const $$_I_BODY_OR_NULL: $$_I_BODY_TYPE | null
declare const $$_I_BODY_OR_EOBJ: $$_I_BODY_TYPE | {}
declare const $$_I_BODY_OR_EARR: $$_I_BODY_TYPE | []
declare const $$_I_BODY: $$_I_BODY_TYPE;

// http method placeholder
declare const $$_HTTP_METHOD_STR: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT";
declare const $$_UPPER_CASE_HTTP_METHOD_STR: typeof $$_HTTP_METHOD_STR;
declare const $$_LOWER_CASE_HTTP_METHOD_STR: "options" | "get" | "head" | "post" | "put" | "delete" | "trace" | "connect";
declare const $$_PASCAL_CASE_HTTP_METHOD_STR: "Options" | "Get" | "Head" | "Post" | "Put" | "Delete" | "Trace" | "Connect";
`;

export async function genPlaceholderDeclare() {
  const workspace = await Helper.workspace().catch(e => { vscode.window.showErrorMessage(e); });
  if (!workspace) { return; }
  const templateFilePath = await ensureTemplateFile();
  const placeholderFilePath = Helper.fsPath(workspace.uri) + '/' + templateFilePath.replace(/(.js|.jsx|.ts|.tsx)$/, '.d.ts');
  return await fs.writeFile(placeholderFilePath, placeholderFileStr);
}
